package com.zyg.cas.config;

/**
 * ------------------------------
 * 作者: LMY
 * 类-功能: spring securityConfig  整合 CAS
 * 创建时间: 2021-11-11 17:36
 * ------------------------------
 **/

import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.validation.Cas20ProxyTicketValidator;
import org.jasig.cas.client.validation.TicketValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.cas.ServiceProperties;
import org.springframework.security.cas.authentication.CasAuthenticationProvider;
import org.springframework.security.cas.web.CasAuthenticationEntryPoint;
import org.springframework.security.cas.web.CasAuthenticationFilter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.logout.LogoutFilter;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;

/**
 * 功能: cas与springsecurity整合的配置类
 * 参数:
 * 返回值:
 * 时间: 2021/8/11 16:53
 */

/**
 * 功能: cas与springsecurity整合的配置类
 * 参数:
 * 返回值:
 * 时间: 2021/8/11 16:53
 */
@Configuration
public class CasSecurityConfig {
    @Autowired
    CASClientProperties casClientProperties;  //导入CAS客户端配置类
    @Autowired
    CASServerProperties casServerProperties;  //导入CAS服务端配置类
    @Autowired
    UserDetailsService userDetailService;     //导入UserDetails接口实现类


    /**
     * 功能: 配置客户端登录成功后的跳转页面
     * 参数:
     * 返回值: org.springframework.security.cas.ServiceProperties
     */
    @Bean
    ServiceProperties serviceProperties() {
        ServiceProperties serviceProperties = new ServiceProperties();
        serviceProperties.setService(casClientProperties.getLogin());
        return serviceProperties;
    }

    /**
     * 功能: 配置验证（cas-server进行验证）的入口
     * 参数:
     * 返回值: org.springframework.security.web.AuthenticationEntryPoint
     * 时间: 2021/8/11 16:59
     */
    @Bean
    @Primary
    AuthenticationEntryPoint authenticationEntryPoint() {
        CasAuthenticationEntryPoint entryPoint = new CasAuthenticationEntryPoint();
        entryPoint.setLoginUrl(casServerProperties.getLogin());
        entryPoint.setServiceProperties(serviceProperties());
        return entryPoint;
    }

    /**
     * 功能: 让cas-server来校验票据
     * 参数:
     * 返回值: org.jasig.cas.client.validation.TicketValidator
     * 时间: 2021/8/11 17:02
     */
    @Bean
    TicketValidator ticketValidator() {
        return new Cas20ProxyTicketValidator(casServerProperties.getPrefix());
    }

    /**
     * 功能: 设置cas认证提供者
     * 参数:
     * 返回值: org.springframework.security.cas.authentication.CasAuthenticationProvider
     * 时间: 2021/8/11 17:04
     */
    @Bean
    CasAuthenticationProvider casAuthenticationProvider() {
        CasAuthenticationProvider provider = new CasAuthenticationProvider();
        provider.setServiceProperties(serviceProperties());
        provider.setTicketValidator(ticketValidator());
        provider.setUserDetailsService(userDetailService);
        provider.setKey("hbxfwf");
        return provider;
    }

    /**
     * 功能: cas认证过滤器：
     * 作用：将请求过滤，拦截后交由CasAuthenticationProvider处理
     * 参数:
     * 返回值: org.springframework.security.cas.web.CasAuthenticationFilter
     * 时间: 2021/8/11 17:04
     */
    @Bean
    CasAuthenticationFilter casAuthenticationFilter(AuthenticationProvider authenticationProvider) {
        CasAuthenticationFilter filter = new CasAuthenticationFilter();
        filter.setServiceProperties(serviceProperties());
        filter.setAuthenticationManager(new ProviderManager(authenticationProvider));
        return filter;
    }

    /**
     * 功能: 通知服务器发出注销请求
     * 参数:
     * 返回值: org.jasig.cas.client.session.SingleSignOutFilter
     * 时间: 2021/8/11 17:06
     */
    @Bean
    SingleSignOutFilter singleSignOutFilter() {
        SingleSignOutFilter sign = new SingleSignOutFilter();
        sign.setIgnoreInitConfiguration(true);
        return sign;
    }

    /**
     * 功能: 将注销请求求转发到服务端
     * 参数:
     * 返回值: org.springframework.security.web.authentication.logout.LogoutFilter
     * 时间: 2021/8/11 17:07
     */
    @Bean
    LogoutFilter logoutFilter() {
        LogoutFilter filter = new LogoutFilter(casServerProperties.getLogout(), new SecurityContextLogoutHandler());
        filter.setFilterProcessesUrl(casClientProperties.getLogoutRelative());
        return filter;
    }

}